package com.anji.captcha.service;

import com.anji.captcha.service.CaptchaCacheService;

import com.neu.utils.SpringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;


import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

//@Service
//@Primary
@Slf4j
@RequiredArgsConstructor
public class CaptchaCacheServiceRedisImpl implements CaptchaCacheService {

    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String type() {
        return "redis";
    }

     @PostConstruct
     public void init() {
         if (stringRedisTemplate == null) {
             stringRedisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
         }
         stringRedisTemplate.opsForValue().set("456", "value", 120, TimeUnit.SECONDS);
     }
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public void set(String key, String value, long expiresInSeconds) {
        stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS);
    }

    @Override
    public boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    @Override
    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public String get(String key) {
        String string = stringRedisTemplate.opsForValue().get(key);
        return string;
    }

    @Override
    public Long increment(String key, long val) {
        return stringRedisTemplate.opsForValue().increment(key, val);
    }

}

